import ToolActionApi from "@/tool/http/tool-action.js";

let deviceAction = ToolActionApi.init("things_device");
let deviceDataItemAction = ToolActionApi.init("things_device_data_item");

const rule = {
	queryParams: [],
	formOnChange: (watchForm) => {
		return [];
	},
	pageConfig: {},
	metaConfig: {},
	baseField: [],
	extendFields: [],
	async listFieldExtend(list = []) {
		let res = await deviceDataItemAction.list({ rules: { data_id__in: list.map(item => item.id).join(",") }});
		
		for(let i = 0; i < list.length; i++) {
			res.filter(item => item.data_id == list[i].id).forEach(dItem => {
				list[i][`_${dItem.factor_code}`] = dItem.factor_val;
				if (!rule.extendFields.find(exItem => exItem.name == `_${dItem.factor_code}`)) {
					rule.extendFields.push({ name: `_${dItem.factor_code}`, memo: dItem.factor_code });
				}
			});
		}
	}
}

export default async function(metadata) {
	rule.metaConfig.device_code = {
		dictList: await deviceAction.dict("name", "code")
	}
	return rule;
}
